<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>throttle</title>
    <style>
        #container{
            width: 100%; height: 200px; line-height: 200px; text-align: center; color: #fff; background-color: #444; font-size: 30px;
        }
    </style>
</head>
<body>
    <div id="container"></div>
    <script>
        /**
          * 节流:每格指定时间段执行一次
        **/
        let container = document.getElementById("container")
        ,count = 1
        ,throttle = function(handle,wait,nowExecute){
            let timer ;
            return function(...args){
                //第一个计时器入栈后，只有等到其被执行完成后，才能执行其他计时器
                if(timer){return}
                timer = setTimeout(()=>{
                    handle.apply(this,args);
                    timer = null;
                },wait);
                if(nowExecute){
                    nowExecute = false;
                    handle.apply(this,args);
                }
            }
        }
        container.addEventListener('mousemove',throttle(function(){
            container.innerHTML = count++;
        },800,true))
    </script>
</body>
</html>

